package test.my.com.myapplicationtest.RefreshScorllView;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import test.my.com.myapplicationtest.recycleview_expand.RefreshHeaderView;

/**
 * Author : Cooper
 * Time : 2017/6/21  17:11
 * Description : 带有下拉刷新的 ScrollView
 *
 * solution : 1.java.lang.IllegalStateException: ScrollView can host only one direct child
 *            ScrollView 只能添加一个ViewGroup
 *
 *            2.onFinishInflate() 当布局文件加载完后调用
 *
 *            2.The specified child already has a parent. " +"You must call removeView() on the child's parent first.
 *            addView前要判断子view是否已经有父view
 */

public class RefreshScrollView extends ScrollView {

    private static final float DRAG_RATE = 3;

    private Context mContext;
    private RefreshHeaderView mRefreshHeaderView;
    private ViewGroup mContainer;
    private boolean isNeedPullDownToRefresh; // 是否需要下来刷新
    private float mLastY = -1;
    private PullDownListener mPullDownListener;

    public void setPullDownListener(PullDownListener pullDownListener) {
        this.mPullDownListener = pullDownListener;
    }

    public RefreshScrollView(Context context) {
        super(context);
        init(context);
    }

    public RefreshScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RefreshScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        initRefreshHeaderView();
        this.isNeedPullDownToRefresh = true;
    }

    public void setIsPullToRefresh(boolean isRefresh) {
        if (isRefresh) {
            initRefreshHeaderView();
        }
        this.isNeedPullDownToRefresh = isRefresh;
    }

    public void setRefreshing(boolean isRefresh) {
        if (isRefresh) {

        } else {
            mRefreshHeaderView.setRefreshStatus(RefreshHeaderView.STATUS_DONE);
        }
    }



    private void initRefreshHeaderView() {
        mRefreshHeaderView = new RefreshHeaderView(mContext);
        mRefreshHeaderView.setRefreshStatus(RefreshHeaderView.STATUS_NORMAL);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int childCount = getChildCount();
        if (childCount > 0) {
            mContainer = (ViewGroup) getChildAt(0);
            mContainer.addView(mRefreshHeaderView,0);
        }

    }

    private boolean isOnTop() {
        return this.getScrollY() == 0;
    }


    @Override
    public boolean onTouchEvent(MotionEvent e) {
        if (mLastY == -1) {
            mLastY = e.getY();
        }
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastY = e.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float deltaY = e.getY() - mLastY;
                mLastY = e.getY();
                if (isOnTop() && isNeedPullDownToRefresh /*&& deltaY > 0*/ && mRefreshHeaderView.mStatus < RefreshHeaderView.STATUS_REFRESHING) {
                    mRefreshHeaderView.headerMove(deltaY/DRAG_RATE);
                    if (mRefreshHeaderView.getVisibleHeight() > 0 ) {
                        return false;
                    }
                }
                break;
            default:
                mLastY = -1;
                if (isOnTop() && isNeedPullDownToRefresh) {
                    if (mRefreshHeaderView.releaseAction()) {
                        if (mPullDownListener != null) {
                            mPullDownListener.onRefresh();
                        }
                    }
                }
                break;
        }
        return super.onTouchEvent(e);
    }

    /**
     * 下拉刷新 回调接口
     */
    public interface PullDownListener {
        void onRefresh();
    }
}
